package com.sf.wisdomkg.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.sf.wisdomkg.R;
import com.sf.wisdomkg.adapter.JianHuRenListViewAdapter;
import com.sf.wisdomkg.utils.CommonUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by Administrator on 2017/9/22.
 */
public class JianHuRenListActivity extends AppCompatActivity implements View.OnClickListener{
    private static final String SHARE_CONTENT = "app_";
    private SharedPreferences setting;


    private Button back_btn;
    private TextView bar_content;
    private ImageView Near_btn;
    private ListView jianhu_list;

    private String baseUrl = CommonUtils.IP_S+ CommonUtils.M_T;

    private String id,name;
    //数据
    private ArrayList<HashMap<String,String>>  jianHuRen_Array;
    private JianHuRenListViewAdapter jianHuRenListViewAdapter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jianhuren_list);
        getSupportActionBar().hide();

        Bundle bundle = this.getIntent().getExtras();
        id = bundle.getString("id");
        name = bundle.getString("name");

        initView();
//        getData();
        GetBabyLine();
        bar_content.setText(name+"的监护人");
//        Near_btn.setVisibility(View.VISIBLE);
        Near_btn.setImageResource(R.mipmap.nav_add_icon);
        back_btn.setOnClickListener(this);
        Near_btn.setOnClickListener(this);
    }

    /**
     * ui初始化
     */
    private void initView(){

        setting = this.getSharedPreferences(SHARE_CONTENT,0);
        if (setting.getString("roleId","").equals("2")){
            baseUrl = CommonUtils.IP_S + CommonUtils.M_T;
        }else if (setting.getString("roleId","").equals("3")){
            baseUrl = CommonUtils.IP_S + CommonUtils.M_S;
        }else if (setting.getString("roleId","").equals("1")){
            baseUrl = CommonUtils.IP_S + CommonUtils.M_T;
        }

        back_btn = (Button)findViewById(R.id.back_btn);
        bar_content = (TextView)findViewById(R.id.bar_content);
        Near_btn = (ImageView)findViewById(R.id.Near_btn);
        jianhu_list = (ListView)findViewById(R.id.jianhu_list);

        jianHuRen_Array = new ArrayList<>();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back_btn:
                finish();
                break;
            case R.id.Near_btn:
                Intent intent = new Intent(JianHuRenListActivity.this,JianHuRenAddActivity.class);
                startActivity(intent);
                break;
        }
    }

//    private void getData(){
//        for (int i = 0; i < 3; i++){
//            HashMap<String,String> hashMap = new HashMap<>();
//            hashMap.put("title","");
//            jianHuRen_Array.add(hashMap);
//        }
//        jianHuRenListViewAdapter = new JianHuRenListViewAdapter(JianHuRenListActivity.this,jianHuRen_Array);
//        jianhu_list.setAdapter(jianHuRenListViewAdapter);
//        jianHuRenListViewAdapter.notifyDataSetChanged();
//    }

    /**
     * 监护人列表
     */
    private void GetBabyLine() {
        RequestParams params = new RequestParams(baseUrl + "parent/parentsList");
        params.addParameter("children",id);
        if (setting.getString("roleId","").equals("2")){
            baseUrl = CommonUtils.IP_S + CommonUtils.M_T;
        }else if (setting.getString("roleId","").equals("3")){
            baseUrl = CommonUtils.IP_S + CommonUtils.M_S;
        }else if (setting.getString("roleId","").equals("1")){
            baseUrl = CommonUtils.IP_S + CommonUtils.M_T;
        }

        //联网设置
        params.setCacheMaxAge(1000*60);
        System.out.println("请求"+params);
        params.setConnectTimeout(90000);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                System.out.println("请求"+result);

                //解析result
                if (result != null){
                    try {
                        JSONObject jsonObject  = new JSONObject(result);
                        if (jsonObject.optString("status").equals("true")){
                            JSONArray jsonArray = jsonObject.getJSONArray("data");
                            if (jsonArray.length() != 0){

                                for (int i = 0; i < jsonArray.length(); i++) {
                                    HashMap<String, String> hashMap = new HashMap<String, String>();
                                    JSONObject jsonObjectSon = new JSONObject();
                                    jsonObjectSon = jsonArray.optJSONObject(i);
                                    hashMap.put("id",jsonObjectSon.optString("id"));
                                    hashMap.put("name",jsonObjectSon.optString("name"));
                                    hashMap.put("age",jsonObjectSon.optString("age"));
                                    hashMap.put("sex",jsonObjectSon.optString("sex"));
                                    hashMap.put("phone",jsonObjectSon.optString("phone"));
                                    hashMap.put("job",jsonObjectSon.optString("job"));
                                    hashMap.put("address",jsonObjectSon.optString("address"));
                                    jianHuRen_Array.add(hashMap);
                                }
                                jianHuRenListViewAdapter = new JianHuRenListViewAdapter(JianHuRenListActivity.this,jianHuRen_Array);
                                jianhu_list.setAdapter(jianHuRenListViewAdapter);
                                jianHuRenListViewAdapter.notifyDataSetChanged();
                            }else {
                                Toast.makeText(JianHuRenListActivity.this, "该学生还没有监护人，快去添加吧！", Toast.LENGTH_SHORT).show();
                            }

                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                System.out.println("请求"+ex);
                Toast.makeText(JianHuRenListActivity.this, "连接服务器失败,请检查网络！", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Toast.makeText(JianHuRenListActivity.this, "请求超时,请检查网络！", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFinished() {

            }
        });
    }
}
